package com.fast.alden.common;

import com.fast.alden.data.model.*;
import com.fast.alden.data.repo.*;
import jakarta.annotation.Resource;
import jakarta.persistence.EntityManager;
import jakarta.transaction.Transactional;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Configuration
@Profile("dev")
public class InitializeTestData implements CommandLineRunner {
    @Resource
    private SysUserRepository userRepository;
    @Resource
    private SysDepartmentRepository departmentRepository;
    @Resource
    private SysRoleRepository roleRepository;
    @Resource
    private SysPositionRepository positionRepository;
    @Resource
    private SysMenuRepository menuRepository;
    @Resource
    private SysMenuRoleRepository menuRoleRepository;
    @Resource
    private SysUserRoleRepository userRoleRepository;
    @Resource
    private SysUserPositionRepository userPositionRepository;
    @Resource
    private EntityManager entityManager;
    @Resource
    private SysApiResourceRepository apiResourceRepository;
    @Resource
    private SysApiResourceRoleRepository apiResourceRoleRepository;
    @Resource
    private SysNoticeRepository noticeRepository;
    @Resource
    private SysMessageRepository messageRepository;
    @Resource
    private PortalUserRepository portalUserRepository;
    private final PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();

    @Value("${fast-alden.secure.default-password}")
    private String originDefaultPassword;
    @Resource
    private RedisTemplate<String, Object> redisTemplate;


    @Override
    @Transactional
    public void run(String... args) throws Exception {
        redisTemplate.delete(redisTemplate.keys("*"));
        execute();
    }

    public void execute() {
        String defaultPassword = passwordEncoder.encode(originDefaultPassword);
        String defaultAvatar = "";

        roleRepository.deleteAll();

        SysRole roleAdmin = new SysRole();
        roleAdmin.setCode("admin");
        roleAdmin.setName("超级管理员");
        roleAdmin.setDescription("超级管理员");
        roleRepository.save(roleAdmin);

        userRepository.deleteAll();

        LocalDateTime now = LocalDateTime.now();

        SysUser userAdmin = new SysUser();
        userAdmin.setUsername("admin");
        userAdmin.setPassword(defaultPassword);
        userAdmin.setNickname("管理员");
        userAdmin.setRealName("管理员");
        userAdmin.setEmail("admin@fa.com");
        userAdmin.setPhone("12345678909");
        userAdmin.setBirthday(now.toLocalDate());
        userAdmin.setIntroduce("超级管理员");
        userAdmin.setAvatar(defaultAvatar);
        userAdmin.setStatus(1);
        userAdmin.setType(0);

        userRepository.save(userAdmin);

        departmentRepository.deleteAll();

        SysDepartment dept1 = new SysDepartment();
        dept1.setCode("001");
        dept1.setName("Fast Alden");
        dept1.setDescription("Fast Alden公司");
        setParent(dept1, null);

        SysDepartment dept2 = new SysDepartment();
        dept2.setCode("002");
        dept2.setName("济南总公司");
        setParent(dept2, dept1);

        SysDepartment dept3 = new SysDepartment();
        dept3.setCode("003");
        dept3.setName("莱芜分公司");
        setParent(dept3, dept1);

        SysDepartment dept4 = new SysDepartment();
        dept4.setCode("004");
        dept4.setName("研发部门");
        setParent(dept4, dept2);

        SysDepartment dept5 = new SysDepartment();
        dept5.setCode("005");
        dept5.setName("市场部门");
        setParent(dept5, dept2);

        SysDepartment dept6 = new SysDepartment();
        entityManager.persist(dept6);
        dept6.setCode("006");
        dept6.setName("测试部门");
        setParent(dept6, dept2);

        SysDepartment dept7 = new SysDepartment();
        entityManager.persist(dept7);
        dept7.setCode("007");
        dept7.setName("财务部门");
        setParent(dept7, dept2);

        SysDepartment dept8 = new SysDepartment();
        entityManager.persist(dept8);
        dept8.setCode("008");
        dept8.setName("运维部门");
        setParent(dept8, dept2);

        SysDepartment dept9 = new SysDepartment();
        dept9.setCode("009");
        dept9.setName("市场部门");
        setParent(dept9, dept3);

        SysDepartment dept10 = new SysDepartment();
        dept10.setCode("010");
        dept10.setName("财务部门");
        setParent(dept10, dept3);

        List<SysDepartment> departments = Arrays.asList(dept1, dept2, dept3, dept4, dept5, dept6, dept7, dept8, dept9, dept10);
        departmentRepository.saveAll(departments);

        SysUser u2 = new SysUser();
        u2.setUsername("zhangsan");
        u2.setPassword(defaultPassword);
        u2.setNickname("张三三");
        u2.setRealName("张三");
        u2.setEmail("zhangsan@fa.com");
        u2.setPhone("12345678910");
        u2.setBirthday(now.toLocalDate());
        u2.setIntroduce(null);
        u2.setAvatar(defaultAvatar);
        u2.setDepartmentId(dept1.getId());
        u2.setDepartmentName(dept1.getName());
        u2.setStatus(1);
        u2.setType(1);

        SysUser u3 = new SysUser();
        u3.setUsername("lisi");
        u3.setPassword(defaultPassword);
        u3.setNickname("李四四");
        u3.setRealName("李四");
        u3.setEmail("lisi@fa.com");
        u3.setPhone("12345678911");
        u3.setBirthday(now.toLocalDate());
        u3.setIntroduce(null);
        u3.setAvatar(defaultAvatar);
        u3.setDepartmentId(dept2.getId());
        u3.setDepartmentName(dept2.getName());
        u3.setStatus(1);
        u3.setType(1);

        SysUser u4 = new SysUser();
        u4.setUsername("wangwu");
        u4.setPassword(defaultPassword);
        u4.setNickname("王五五");
        u4.setRealName("王五");
        u4.setEmail("wangwu@fa.com");
        u4.setPhone("12345678912");
        u4.setBirthday(now.toLocalDate());
        u4.setIntroduce(null);
        u4.setAvatar(defaultAvatar);
        u4.setDepartmentId(dept3.getId());
        u4.setDepartmentName(dept3.getName());
        u4.setStatus(1);
        u4.setType(1);

        SysUser u5 = new SysUser();
        u5.setUsername("zhaoliu");
        u5.setPassword(defaultPassword);
        u5.setNickname("赵六六");
        u5.setRealName("赵六");
        u5.setEmail("zhaoliu@fa.com");
        u5.setPhone("12345789013");
        u5.setBirthday(now.toLocalDate());
        u5.setIntroduce(null);
        u5.setAvatar(defaultAvatar);
        u5.setDepartmentId(dept4.getId());
        u5.setDepartmentName(dept4.getName());
        u5.setStatus(1);
        u5.setType(1);

        SysUser u6 = new SysUser();
        u6.setUsername("fengqi");
        u6.setPassword(defaultPassword);
        u6.setNickname("冯七七");
        u6.setRealName("冯七");
        u6.setEmail("fengqi@fa.com");
        u6.setPhone("12345678914");
        u6.setBirthday(now.toLocalDate());
        u6.setIntroduce(null);
        u6.setAvatar(defaultAvatar);
        u6.setDepartmentId(dept5.getId());
        u6.setDepartmentName(dept5.getName());
        u6.setStatus(1);
        u6.setType(1);

        SysUser u7 = new SysUser();
        u7.setUsername("weiba");
        u7.setPassword(defaultPassword);
        u7.setNickname("魏八八");
        u7.setRealName("魏八");
        u7.setEmail("weiba@fa.com");
        u7.setPhone("123455678915");
        u7.setBirthday(now.toLocalDate());
        u7.setIntroduce(null);
        u7.setAvatar(defaultAvatar);
        u7.setDepartmentId(dept6.getId());
        u7.setDepartmentName(dept6.getName());
        u7.setStatus(1);
        u7.setType(1);

        SysUser u8 = new SysUser();
        u8.setUsername("chujiu");
        u8.setPassword(defaultPassword);
        u8.setNickname("楚九九");
        u8.setRealName("楚九");
        u8.setEmail("chujiu@fa.com");
        u8.setPhone("123455678915");
        u8.setBirthday(now.toLocalDate());
        u8.setIntroduce(null);
        u8.setAvatar(defaultAvatar);
        u8.setDepartmentId(dept7.getId());
        u8.setDepartmentName(dept7.getName());
        u8.setStatus(1);
        u8.setType(1);

        List<SysUser> userList = List.of(u2, u3, u4, u5, u6, u7, u8);
        userRepository.saveAll(userList);

        List<SysUser> users = new ArrayList<>();
        for (int i = 1; i <= 100; i++) {
            SysUser u = new SysUser();
            u.setUsername("u" + i);
            u.setPassword(defaultPassword);
            u.setNickname("un" + i);
            u.setRealName("ur" + i);
            u.setEmail("u" + i + "@fa.com");
            u.setPhone("123455678916");
            u.setBirthday(now.toLocalDate());
            u.setIntroduce(null);
            u.setAvatar(defaultAvatar);
            u.setDepartmentId(dept4.getId());
            u.setDepartmentName(dept4.getName());
            u.setStatus(1);
            u.setType(1);

            users.add(u);
        }
        userRepository.saveAll(users);

        SysRole role1 = new SysRole();
        role1.setCode("tester");
        role1.setName("测试用户");
        role1.setDescription("测试角色");

        SysRole role2 = new SysRole();
        role2.setCode("developer");
        role2.setName("开发用户");
        role2.setDescription("开发角色");

        SysRole role3 = new SysRole();
        role3.setCode("user");
        role3.setName("普通用户");
        role3.setDescription("普通角色");

        List<SysRole> roles = List.of(role1, role2, role3);
        roleRepository.saveAll(roles);

        positionRepository.deleteAll();

        SysPosition pos1 = new SysPosition();
        pos1.setCode("president");
        pos1.setName("董事长");
        pos1.setDescription("董事长");

        SysPosition pos2 = new SysPosition();
        pos2.setCode("ceo");
        pos2.setName("总经理");
        pos2.setDescription("总经理");

        SysPosition pos3 = new SysPosition();
        pos3.setCode("hr");
        pos3.setName("人力资源");
        pos3.setDescription("人力资源");

        SysPosition pos4 = new SysPosition();
        pos4.setCode("developer");
        pos4.setName("软件研发");
        pos4.setDescription("软件研发");

        SysPosition pos5 = new SysPosition();
        pos5.setCode("test");
        pos5.setName("软件测试");
        pos5.setDescription("软件测试");

        SysPosition pos6 = new SysPosition();
        pos6.setCode("operation");
        pos6.setName("软件运维");
        pos6.setDescription("软件运维");

        SysPosition pos7 = new SysPosition();
        pos7.setCode("sales");
        pos7.setName("销售");
        pos7.setDescription("销售");

        SysPosition pos8 = new SysPosition();
        pos8.setCode("financial");
        pos8.setName("财务");
        pos8.setDescription("财务");

        List<SysPosition> positions = List.of(pos1, pos2, pos3, pos4, pos5, pos6, pos7, pos8);
        positionRepository.saveAll(positions);

        menuRepository.deleteAll();

        SysMenu menu0 = new SysMenu();
        menu0.setCode("Welcome");
        menu0.setName("首页");
        menu0.setDescription("首页");
        menu0.setType(SysMenuType.MENU);
        menu0.setIcon("Odometer");
        menu0.setDisplay(true);
        menu0.setRoute("/welcome");
        menu0.setMode(SysMenuMode.SYNC_ROUTE);
        menu0.setSelector(null);
        menu0.setOrderNo(1);
        setParent(menu0, null);

        SysMenu menu1 = new SysMenu();
        menu1.setCode("PortalManagement");
        menu1.setName("业务管理");
        menu1.setDescription("业务管理");
        menu1.setType(SysMenuType.DIRECTORY);
        menu1.setIcon("Coin");
        menu1.setDisplay(true);
        menu1.setOrderNo(1);
        setParent(menu1, null);

        SysMenu menu1_1 = new SysMenu();
        menu1_1.setCode("PortalUserManagement");
        menu1_1.setName("前台用户管理");
        menu1_1.setDescription("前台用户管理");
        menu1_1.setType(SysMenuType.MENU);
        menu1_1.setIcon("User");
        menu1_1.setDisplay(true);
        menu1_1.setRoute("/portal-user");
        menu1_1.setMode(SysMenuMode.SYNC_ROUTE);
        menu1_1.setPath(null);
        menu1_1.setSelector(null);
        menu1_1.setOrderNo(1);
        setParent(menu1_1, menu1);

        SysMenu menu2 = new SysMenu();
        menu2.setCode("SystemManagement");
        menu2.setName("系统管理");
        menu2.setDescription("系统管理");
        menu2.setType(SysMenuType.DIRECTORY);
        menu2.setIcon("Setting");
        menu2.setDisplay(true);
        menu2.setOrderNo(2);
        setParent(menu2, null);

        SysMenu menu2_1 = new SysMenu();
        menu2_1.setCode("UserManagement");
        menu2_1.setName("用户管理");
        menu2_1.setDescription("用户管理");
        menu2_1.setType(SysMenuType.MENU);
        menu2_1.setIcon("User");
        menu2_1.setDisplay(true);
        menu2_1.setRoute("/user");
        menu2_1.setMode(SysMenuMode.SYNC_ROUTE);
        menu2_1.setPath(null);
        menu2_1.setSelector(null);
        menu2_1.setOrderNo(3);
        setParent(menu2_1, menu2);

        SysMenu menu2_2 = new SysMenu();
        menu2_2.setCode("UserDetail");
        menu2_2.setName("用户详情");
        menu2_2.setDescription("用户详情");
        menu2_2.setType(SysMenuType.MENU);
        menu2_2.setIcon(null);
        menu2_2.setDisplay(false);
        menu2_2.setRoute("/user-detail");
        menu2_2.setMode(SysMenuMode.SYNC_ROUTE);
        menu2_2.setPath(null);
        menu2_2.setSelector(null);
        menu2_2.setOrderNo(4);
        setParent(menu2_2, menu2);

        SysMenu menu2_3 = new SysMenu();
        menu2_3.setCode("RoleManagement");
        menu2_3.setName("角色管理");
        menu2_3.setDescription("角色管理");
        menu2_3.setType(SysMenuType.MENU);
        menu2_3.setIcon("Avatar");
        menu2_3.setDisplay(true);
        menu2_3.setRoute("/role");
        menu2_3.setMode(SysMenuMode.SYNC_ROUTE);
        menu2_3.setPath(null);
        menu2_3.setSelector(null);
        menu2_3.setOrderNo(5);
        setParent(menu2_3, menu2);

        SysMenu menu2_4 = new SysMenu();
        menu2_4.setCode("DepartmentManagement");
        menu2_4.setName("部门管理");
        menu2_4.setDescription("部门管理");
        menu2_4.setType(SysMenuType.MENU);
        menu2_4.setIcon("Grid");
        menu2_4.setDisplay(true);
        menu2_4.setRoute("/department");
        menu2_4.setMode(SysMenuMode.SYNC_ROUTE);
        menu2_4.setPath(null);
        menu2_4.setSelector(null);
        menu2_4.setOrderNo(6);
        setParent(menu2_4, menu2);

        SysMenu menu2_5 = new SysMenu();
        menu2_5.setCode("PositionManagement");
        menu2_5.setName("岗位管理");
        menu2_5.setDescription("岗位管理");
        menu2_5.setType(SysMenuType.MENU);
        menu2_5.setIcon("Position");
        menu2_5.setDisplay(true);
        menu2_5.setRoute("/position");
        menu2_5.setMode(SysMenuMode.SYNC_ROUTE);
        menu2_5.setPath(null);
        menu2_5.setSelector(null);
        menu2_5.setOrderNo(7);
        setParent(menu2_5, menu2);

        SysMenu menu2_6 = new SysMenu();
        menu2_6.setCode("MenuManagement");
        menu2_6.setName("菜单管理");
        menu2_6.setDescription("菜单管理");
        menu2_6.setType(SysMenuType.MENU);
        menu2_6.setIcon("Pointer");
        menu2_6.setDisplay(true);
        menu2_6.setRoute("/menu");
        menu2_6.setMode(SysMenuMode.SYNC_ROUTE);
        menu2_6.setPath(null);
        menu2_6.setSelector(null);
        menu2_6.setOrderNo(8);
        setParent(menu2_6, menu2);

        SysMenu menu2_7 = new SysMenu();
        menu2_7.setCode("ApiResourceManagement");
        menu2_7.setName("API资源管理");
        menu2_7.setDescription("API资源管理");
        menu2_7.setType(SysMenuType.MENU);
        menu2_7.setIcon("Operation");
        menu2_7.setDisplay(true);
        menu2_7.setRoute("/api-resource");
        menu2_7.setMode(SysMenuMode.SYNC_ROUTE);
        menu2_7.setPath(null);
        menu2_7.setSelector(null);
        menu2_7.setOrderNo(9);
        setParent(menu2_7, menu2);

        SysMenu menu2_8 = new SysMenu();
        menu2_8.setCode("NoticeManagement");
        menu2_8.setName("通知公告管理");
        menu2_8.setDescription("通知公告管理");
        menu2_8.setType(SysMenuType.MENU);
        menu2_8.setIcon("Bell");
        menu2_8.setDisplay(true);
        menu2_8.setRoute("/notice");
        menu2_8.setMode(SysMenuMode.SYNC_ROUTE);
        menu2_8.setPath(null);
        menu2_8.setSelector(null);
        menu2_8.setOrderNo(10);
        setParent(menu2_8, menu2);

        SysMenu menu2_9 = new SysMenu();
        menu2_9.setCode("RoleDetail");
        menu2_9.setName("角色详情");
        menu2_9.setDescription("角色详情");
        menu2_9.setType(SysMenuType.MENU);
        menu2_9.setIcon(null);
        menu2_9.setDisplay(false);
        menu2_9.setRoute("/role-detail");
        menu2_9.setMode(SysMenuMode.SYNC_ROUTE);
        menu2_9.setPath(null);
        menu2_9.setSelector(null);
        menu2_9.setOrderNo(11);
        setParent(menu2_9, menu2);


        SysMenu menu3 = new SysMenu();
        menu3.setCode("SystemMonitor");
        menu3.setName("系统监控");
        menu3.setDescription("系统监控");
        menu3.setType(SysMenuType.DIRECTORY);
        menu3.setIcon("Compass");
        menu3.setDisplay(true);
        menu3.setOrderNo(12);
        setParent(menu3, null);

        SysMenu menu3_1 = new SysMenu();
        menu3_1.setCode("OnlineUserManagement");
        menu3_1.setName("在线用户");
        menu3_1.setDescription("在线用户");
        menu3_1.setType(SysMenuType.MENU);
        menu3_1.setIcon("UserFilled");
        menu3_1.setDisplay(true);
        menu3_1.setRoute("/online-user");
        menu3_1.setMode(SysMenuMode.SYNC_ROUTE);
        menu3_1.setPath(null);
        menu3_1.setSelector(null);
        menu3_1.setOrderNo(13);
        setParent(menu3_1, menu3);

        SysMenu menu3_3 = new SysMenu();
        menu3_3.setCode("LogManagement");
        menu3_3.setName("日志管理");
        menu3_3.setDescription("日志管理");
        menu3_3.setType(SysMenuType.DIRECTORY);
        menu3_3.setIcon("EditPen");
        menu3_3.setDisplay(true);
        menu3_3.setOrderNo(14);
        setParent(menu3_3, menu3);

        SysMenu menu3_3_1 = new SysMenu();
        menu3_3_1.setCode("LoginLogManagement");
        menu3_3_1.setName("登录日志");
        menu3_3_1.setDescription("登录日志");
        menu3_3_1.setType(SysMenuType.MENU);
        menu3_3_1.setIcon("Memo");
        menu3_3_1.setDisplay(true);
        menu3_3_1.setRoute("/login-log");
        menu3_3_1.setMode(SysMenuMode.SYNC_ROUTE);
        menu3_3_1.setPath(null);
        menu3_3_1.setSelector(null);
        menu3_3_1.setOrderNo(15);
        setParent(menu3_3_1, menu3_3);

        SysMenu menu3_3_3 = new SysMenu();
        menu3_3_3.setCode("OperationLogManagement");
        menu3_3_3.setName("操作日志");
        menu3_3_3.setDescription("操作日志");
        menu3_3_3.setType(SysMenuType.MENU);
        menu3_3_3.setIcon("Notebook");
        menu3_3_3.setDisplay(true);
        menu3_3_3.setRoute("/operation-log");
        menu3_3_3.setMode(SysMenuMode.SYNC_ROUTE);
        menu3_3_3.setPath(null);
        menu3_3_3.setSelector(null);
        menu3_3_3.setOrderNo(16);
        setParent(menu3_3_3, menu3_3);

        SysMenu menu3_4 = new SysMenu();
        menu3_4.setCode("MessageManagement");
        menu3_4.setName("消息管理");
        menu3_4.setDescription("消息管理");
        menu3_4.setType(SysMenuType.MENU);
        menu3_4.setIcon("Message");
        menu3_4.setDisplay(true);
        menu3_4.setRoute("/message");
        menu3_4.setMode(SysMenuMode.SYNC_ROUTE);
        menu3_4.setPath(null);
        menu3_4.setSelector(null);
        menu3_4.setOrderNo(17);
        setParent(menu3_4, menu3);

        SysMenu menu3_5 = new SysMenu();
        menu3_5.setCode("MessageManagement");
        menu3_5.setName("服务监控");
        menu3_5.setDescription("服务监控");
        menu3_5.setType(SysMenuType.MENU);
        menu3_5.setIcon("Monitor");
        menu3_5.setDisplay(true);
        menu3_5.setRoute("/server");
        menu3_5.setMode(SysMenuMode.SYNC_ROUTE);
        menu3_5.setPath(null);
        menu3_5.setSelector(null);
        menu3_5.setOrderNo(18);
        setParent(menu3_5, menu3);

        SysMenu menu4 = new SysMenu();
        menu4.setCode("PersonalProfile");
        menu4.setName("个人中心");
        menu4.setDescription("个人中心");
        menu4.setType(SysMenuType.DIRECTORY);
        menu4.setIcon("Setting");
        menu4.setDisplay(true);
        menu4.setOrderNo(19);
        setParent(menu4, null);

        SysMenu menu4_1 = new SysMenu();
        menu4_1.setCode("Profile");
        menu4_1.setName("个人信息");
        menu4_1.setDescription("个人信息");
        menu4_1.setType(SysMenuType.MENU);
        menu4_1.setIcon("Female");
        menu4_1.setDisplay(true);
        menu4_1.setRoute("/profile");
        menu4_1.setMode(SysMenuMode.SYNC_ROUTE);
        menu4_1.setPath(null);
        menu4_1.setSelector(null);
        menu4_1.setOrderNo(20);
        setParent(menu4_1, menu4);

        SysMenu menu4_2 = new SysMenu();
        menu4_2.setCode("ChangePassword");
        menu4_2.setName("修改密码");
        menu4_2.setDescription("修改密码");
        menu4_2.setType(SysMenuType.MENU);
        menu4_2.setIcon("Lock");
        menu4_2.setDisplay(true);
        menu4_2.setRoute("/change-password");
        menu4_2.setMode(SysMenuMode.SYNC_ROUTE);
        menu4_2.setPath(null);
        menu4_2.setSelector(null);
        menu4_2.setOrderNo(21);
        setParent(menu4_2, menu4);

        SysMenu menu5 = new SysMenu();
        menu5.setCode("MessageCenter");
        menu5.setName("消息中心");
        menu5.setDescription("消息中心");
        menu5.setType(SysMenuType.MENU);
        menu5.setIcon("MessageBox");
        menu5.setDisplay(true);
        menu5.setRoute("/message-center");
        menu5.setMode(SysMenuMode.SYNC_ROUTE);
        menu5.setPath(null);
        menu5.setSelector(null);
        menu5.setOrderNo(18);
        setParent(menu5, null);

        SysMenu menu6 = new SysMenu();
        menu6.setCode("TestPage");
        menu6.setName("测试页面");
        menu6.setDescription("测试页面");
        menu6.setType(SysMenuType.DIRECTORY);
        menu6.setIcon("WarnTriangleFilled");
        menu6.setDisplay(true);
        menu6.setOrderNo(19);
        setParent(menu6, null);

        SysMenu menu6_1 = new SysMenu();
        menu6_1.setCode("FastAldenPortal");
        menu6_1.setName("快安官网（外链）");
        menu6_1.setDescription("快安官网");
        menu6_1.setType(SysMenuType.MENU);
        menu6_1.setIcon(null);
        menu6_1.setDisplay(true);
        menu6_1.setUrl("https://tech.zc1288.com");
        menu6_1.setMode(SysMenuMode.EXTERNAL_LINK);
        menu6_1.setPath(null);
        menu6_1.setSelector(null);
        menu6_1.setOrderNo(19);
        setParent(menu6_1, menu6);

        SysMenu menu6_2 = new SysMenu();
        menu6_2.setCode("FastAldenPortal2");
        menu6_2.setName("快安官网（内链）");
        menu6_2.setDescription("快安官网");
        menu6_2.setType(SysMenuType.MENU);
        menu6_2.setIcon(null);
        menu6_2.setDisplay(true);
        menu6_2.setUrl("https://tech.zc1288.com");
        menu6_2.setMode(SysMenuMode.INTERNAL_LINK);
        menu6_2.setPath(null);
        menu6_2.setSelector(null);
        menu6_2.setOrderNo(20);
        setParent(menu6_2, menu6);

        SysMenu menu6_3 = new SysMenu();
        menu6_3.setCode("AsyncRoute");
        menu6_3.setName("异步路由");
        menu6_3.setDescription("异步路由");
        menu6_3.setType(SysMenuType.MENU);
        menu6_3.setIcon(null);
        menu6_3.setDisplay(true);
        menu6_3.setRoute("/async-route");
        menu6_3.setMode(SysMenuMode.ASYNC_ROUTE);
        menu6_3.setPath("../views/test/AsyncRoute.vue");
        menu6_3.setSelector(null);
        menu6_3.setOrderNo(21);
        setParent(menu6_3, menu6);

        List<SysMenu> menus = List.of(
                menu0,
                menu1, menu1_1,
                menu2, menu2_1, menu2_2, menu2_3, menu2_4, menu2_5, menu2_6, menu2_7, menu2_8, menu2_9,
                menu3, menu3_1, menu3_3, menu3_3_1, menu3_3_3, menu3_4, menu3_5,
                menu4, menu4_1, menu4_2,
                menu5,
                menu6, menu6_1, menu6_2, menu6_3
        );
        menuRepository.saveAll(menus);

        List<SysMenu> menusAllCanEnter = List.of(menu0,
                menu4, menu4_1, menu4_2,
                menu5, menu6,
                menu6_1, menu6_2, menu6_3);

        SysUserPosition userPosition1 = new SysUserPosition();
        userPosition1.setUserId(u2.getId());
        userPosition1.setPositionId(pos1.getId());

        SysUserPosition userPosition2 = new SysUserPosition();
        userPosition2.setUserId(u2.getId());
        userPosition2.setPositionId(pos2.getId());

        SysUserPosition userPosition3 = new SysUserPosition();
        userPosition3.setUserId(u3.getId());
        userPosition3.setPositionId(pos3.getId());

        SysUserPosition userPosition4 = new SysUserPosition();
        userPosition4.setUserId(u7.getId());
        userPosition4.setPositionId(pos4.getId());

        SysUserPosition userPosition5 = new SysUserPosition();
        userPosition5.setUserId(u8.getId());
        userPosition5.setPositionId(pos8.getId());

        List<SysUserPosition> userPositions = List.of(userPosition1, userPosition2, userPosition3, userPosition4, userPosition5);
        userPositionRepository.saveAll(userPositions);

        // 管理员角色所有菜单都可访问
        List<SysMenuRole> menuRolesAdmin = menus.stream().map(menu -> {
            SysMenuRole menuRole = new SysMenuRole();
            menuRole.setMenuId(menu.getId());
            menuRole.setRoleId(roleAdmin.getId());
            return menuRole;
        }).toList();

        // 测试用户角色所有菜单都可访问
        List<SysMenuRole> menuRoles1 = menus.stream().map(menu -> {
            SysMenuRole menuRole = new SysMenuRole();
            menuRole.setMenuId(menu.getId());
            menuRole.setRoleId(role1.getId());
            return menuRole;
        }).toList();

        // 开发用户角色所有菜单都可访问
        List<SysMenuRole> menuRoles2 = menus.stream().map(menu -> {
            SysMenuRole menuRole = new SysMenuRole();
            menuRole.setMenuId(menu.getId());
            menuRole.setRoleId(role2.getId());
            return menuRole;
        }).toList();

        // 普通用户角色部分菜单可访问
        List<SysMenuRole> menuRoles3 = menusAllCanEnter.stream().map(menu -> {
            SysMenuRole menuRole = new SysMenuRole();
            menuRole.setMenuId(menu.getId());
            menuRole.setRoleId(role3.getId());
            return menuRole;
        }).toList();

        menuRoleRepository.saveAll(menuRolesAdmin);
        menuRoleRepository.saveAll(menuRoles1);
        menuRoleRepository.saveAll(menuRoles2);
        menuRoleRepository.saveAll(menuRoles3);

        SysUserRole userRole1 = new SysUserRole();
        userRole1.setRoleId(roleAdmin.getId());
        userRole1.setUserId(userAdmin.getId());

        SysUserRole userRole2 = new SysUserRole();
        userRole2.setUserId(u2.getId());
        userRole2.setRoleId(role1.getId());

        SysUserRole userRole3 = new SysUserRole();
        userRole3.setUserId(u3.getId());
        userRole3.setRoleId(role2.getId());

        SysUserRole userRole4 = new SysUserRole();
        userRole4.setUserId(u4.getId());
        userRole4.setRoleId(role3.getId());

        List<SysUserRole> userRoles = List.of(userRole1, userRole2, userRole3, userRole4);
        userRoleRepository.saveAll(userRoles);

//        // 预置管理员角色可访问所有api资源的权限
//        SysApiResource resourceAll = new SysApiResource();
//        resourceAll.setName("All");
//        resourceAll.setUrl("/**");
//        resourceAll.setRequireLogin(true);
//        resourceAll.setRequirePermissions(true);
//
//        apiResourceRepository.save(resourceAll);
//
//        SysApiResourceRole apiResourceRoleAll = new SysApiResourceRole();
//        apiResourceRoleAll.setApiResourceId(resourceAll.getId());
//        apiResourceRoleAll.setRoleId(roleAdmin.getId());
//
//        apiResourceRoleRepository.save(apiResourceRoleAll);

        SysNotice notice1 = new SysNotice();
        notice1.setTitle("系统上线");
        notice1.setContent("快安权限管理系统上线啦！");
        notice1.setPublished(false);
        notice1.setCreatorId(userAdmin.getId());
        notice1.setCreatorName(userAdmin.getNickname());
        notice1.setLastModifierId(userAdmin.getId());
        notice1.setLastModifierName(userAdmin.getNickname());

        noticeRepository.save(notice1);

        PortalUser portalUser1 = new PortalUser();
        portalUser1.setUsername("pu1");
        portalUser1.setPassword(defaultPassword);
        portalUser1.setNickname("Portal User1");
        portalUser1.setRealName("Portal 1");
        portalUser1.setEmail("pu1@fa.com");
        portalUser1.setPhone("12345678910");
        portalUser1.setBirthday(now.toLocalDate());
        portalUser1.setIntroduce("");
        portalUser1.setAvatar(defaultAvatar);
        portalUser1.setStatus(1);

        portalUser1 = portalUserRepository.save(portalUser1);

    }

    private void setParent(ITreeNode node, ITreeNode parent) {
        entityManager.persist(node);
        if (parent != null) {
            node.setParentId(parent.getId());
            node.setRootId(parent.getRootId());
            node.setIdPath(parent.getIdPath() + ITreeNode.ID_PATH_SEPARATOR + node.getId());
        } else {
            node.setParentId(null);
            node.setRootId(node.getId());
            node.setIdPath(ITreeNode.ID_PATH_SEPARATOR + node.getId());
        }
    }
}
